/**
 * Created by TOPFEEL on 2018/2/24.
 */

layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    //表单重新渲染
    form.render();

    //表格渲染
    var tableObj = table.render({
        id: 'list_table'
        , elem: '#list_table'
        , url: '/sys/dict/list'
        , align: "center"
        , cols: [[ //表头
            {type:'numbers',title:"序号", width: 50}
            , {field: 'dictName', title: '字典名称', width: 140, align: 'center'}
            , {field: 'dictKey', title: '字典key值', width: 100, align: 'center'}
            , {field: 'dictType', title: '字典分类', width: 100, align: 'center'}
            , {field: 'dictOrder', title: '排序', width: 70, align: 'center'}
            , {field: 'isLeaf', title: '叶子节点', width: 90,templet:function(d){
                return d.isLeaf == 0 ? '否':'是'
            }, align: 'center'}
            , {field: 'dictState', title: '状态', width: 70,templet:function(d){
                return d.dictState == 0 ? '禁用':'启用'
            }, align: 'center'}
            , {field: 'dictCol1', title: '备用字段1', width: 100, align: 'center'}
            , {field: 'dictCol2', title: '备用字段2', width: 100, align: 'center'}
            , {field: 'dictCol3', title: '备用字段3', width: 100, align: 'center'}
            , {field: 'createTime', title: '添加时间', width: 170, align: 'center'}
            , {field: 'lock', title: '操作', width: 150, templet: function (d) {
                return d.isSystem=='1'?'<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled">修改</button>'+
                '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled">删除</button>':
                    '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>'+
                    '<button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">删除</button>';
            }, unresize: true, align: 'center'}
        ]],where: {"parentNo": "0"}
    });

    //监听工具条
    table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值

        if (layEvent === 'edit') {
            showDialog({
                title: '修改'
                , template: 'edit'
                , saveUrl: '/sys/dict/save'
                , tableObj: tableObj
                , width: 500
                , height: 600
                , htmlData: data
                ,submited: function(){
                    getZtree();
                }
            })
        }
        if (layEvent === 'delete') {
            var obj = {"dictNo": data.dictNo};
            showConfirm('确认删除？',function () {
                $.ajax({
                    type: "GET",
                    url: '/sys/dict/delete',
                    data: obj,
                    success: function (result) {
                        result.msg && layer.msg(result.msg);
                        if (result.success) {
                            reloadTable(tableObj);
                            getZtree();
                        }
                    },

                    error: function () {
                        layer.msg("删除异常");
                    }
                })
            },0);
        }
    });
    // 顶部 [添加]和[删除]操作事件
    $("#dict_add").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length==0){
            layer.msg("请选择节点");
            return;
        }
        var node=nodes[0];

        showDialog({
            title: '添加'
            , template: 'dic-add'
            , saveUrl: '/sys/dict/save'
            , tableObj: tableObj
            , width: 600
            , height: 600
            , htmlData:node
            ,afterSubmit: function(){
                getZtree();
            }
        });
    });


    //修改
    $("#dict_edit").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length==0){
            layer.msg("请选择节点");
            return;
        }
        var node=nodes[0];
        if(node.level==0){
            layer.msg("根节点不能修改");
            return;
        }
        if(node.attrs.sysFlag == '1'){
            layer.msg("系统数据禁止修改");
            return;
        }
        $.ajax({
            url: '/sys/dict/getsysdict?dictNo='+node.id,
            contentType: 'application/json',
            success: function (result) {
                if(result && result.data){

                    showDialog({
                        title: '修改'
                        , template: 'dic-edit'
                        , saveUrl: '/sys/dict/save'
                        , tableObj: tableObj
                        , width: 600
                        , height: 600
                        , htmlData: result.data
                        ,submited: function(){
                            getZtree();
                        }
                    });
                }
            },
            error: function () {
                layer.msg("操作异常");
            }
        });


    });


    //删除
    $("#dict_delete").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length==0){
            layer.msg("请选择节点");
            return;
        }
        var node=nodes[0];
        if(node.level==0){
            layer.msg("根节点不能删除");
            return;
        }
        if(node.attrs.sysFlag == '1'){
            layer.msg("系统数据禁止删除");
            return;
        }
        if(node.isParent){
            layer.msg("请先删除子节点");
            return;
        }
        var obj = {"dictNo": node.id};
        showConfirm('确认删除？',function () {
            $.ajax({
                type: "GET",
                url: '/sys/dict/delete',
                data: obj,
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.success) {
                        reloadTable(tableObj);
                        getZtree();
                    }
                },
                error: function () {
                    layer.msg("删除异常");
                }
            })
        },0);
    });

    //查询数据
    $('#query').on('click', function () {
        reloadTable(tableObj);
    });


    var ztreeObj=null;
    var setting = {
        data: {
            key: {
                title: "t"
            },
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: true,
            showTitle: false
        },
        callback: {
            onClick: ztreeCallback
        }
    };


    //获取左侧ztree
    getZtree();

    function getZtree() {
        $.ajax({
            url: '/sys/dict/dicttree',
            success: function (result) {
              if (result && result.data) {
                ztreeObj=initZtree("ztree", setting, result.data);
              }else{
                layer.msg("数据异常");
              }
            }
        });
    }

    function ztreeCallback(event, treeId, treeNode) {
        var parentFlag = treeNode.isParent;
        var nodeId = treeNode.id;
        if(parentFlag){
            $("#list_form input[name='parentNo']").val(nodeId);
            $("#list_form input[name='dictNo']").val("");
        }else{
            $("#list_form input[name='parentNo']").val("");
            $("#list_form input[name='dictNo']").val(nodeId);
        }
        reloadTable(tableObj);
    }
});